var results_div = "search_results_area";
var info_div = "info_div";
var sort_col ="last_name";
var sort_dir ="desc";

var progress_bar = function(wd,ht,txt,cl) {
	var throwaway = '<img src="/images/progress.gif" id="progress_bar" width="'+wd+'" height="'+ht+'" alt="'+txt+'" title="'+txt+'" align="absmiddle" style="padding:3px;"/>';	
	return throwaway;
};
var loading_bar = function(x,ele) {
	(x) ? $("#"+ele).html(progress_bar(16,16,'','blue')) : $("#"+ele).html('');
};


var searchPoc = function(clicked_action,sort_col,sort_dir) {		
	$('#'+results_div).html('<table id="" border="1" cellspacing="0" cellpadding="0" class="datagrid"><thead><tr><th  width="2%"><input type="checkbox" id="select_all" name="select_all"  /></th><th><div id="sort_state">Location</div></th><th ><div id="sort_city">Jurisdiction</div></th><th  ><div id="sort_gov_type">Agency Type</div></th><th><div id="sort_gov_type_name">Agency Name</div></th><th ><div id="sort_address">Agency Physical Address</div></th><th ><div id="sort_last_name">Contact Last Name</div></th><th ><div id="sort_first_name">Contact First Name</div></th><th ><div id="sort_title">Contact Title</div></th><th ><div id="sort_email">Contact Email</div></th><th ><div >Contact Phone </div></th></tr></thead><tbody><tr><td colspan="11" id="loading_area"><div align="center" class="Content_Margin">Loading Information<br />'+progress_bar(32,32,'','')+'</div></td></tr></table>');	

	$('#sort_').val( sort_col);
	$('#sort_direction').val(sort_dir);
	sort_col = $('#sort_').val( );
	sort_dir = $('#sort_direction').val();
	
    var tmp = $('#sort_direction').val();
		if (tmp == "desc")
		{
			$('#sort_direction').val("asc") ;
		}
		else 
		{
			$('#sort_direction').val("desc");
		}

		$.ajax({
		type: 'GET',
		url: "/api/ajax_admin_delete_poc.cfm?action="+clicked_action+""+
		"&search_term_2="+$('#name').val()+
		"&lowest_term="+$('#lowest_term').val()+
		"&highest_term="+$('#highest_term').val()+
		"&count="+$('#count').val()+
		"&sort_column="+$('#sort_').val()+
		"&sort_direction="+$('#sort_direction').val(),
     dataType: "json",
     success: function(json) {
		var json_data = [];
		var json_search_data = [];
		json_data = json.result;
		json_search_data = json.search_info;
		
		results_text = '';
		
		results_text += '<table id="" border="1" cellspacing="0" cellpadding="0" class="datagrid"><thead><tr><th  width="2%"><input type="checkbox" id="select_all" name="select_all" class="checkbox_results" /></th><th><div id="sort_state">Location</div></th><th ><div id="sort_city">Jurisdiction</div></th><th  ><div id="sort_gov_type">Agency Type</div></th><th><div id="sort_gov_type_name">Agency Name</div></th><th ><div id="sort_address">Agency Physical Address</div></th><th ><div id="sort_last_name">Contact Last Name</div></th><th ><div id="sort_first_name">Contact First Name</div></th><th ><div id="sort_title">Contact Title</div></th><th ><div id="sort_email">Contact Email</div></th><th ><div id="sort_phone">Contact Phone </div></th></tr></thead>';

        $.map(json_data,function(json_data){    
				results_text += '<tbody><tr><td align="center" valign="absmiddle"><input type="checkbox" class="checkbox_results" id="_filter_' +  json_data.poc_id + '" name="_filter_' +  json_data.poc_id + '" value="' +  json_data.poc_id + '"/></td><td>' +  json_data.column_state + '&nbsp;</td><td>' +  json_data.column_city + '&nbsp;</td><td>' +  json_data.column_gov_type + '&nbsp;</td><td>' +  json_data.column_company + '&nbsp;</td><td>' +  json_data.column_address + '&nbsp;</td><td><a target="_blank" href="/edit.cfm?id=' + json_data.poc_id + '" >' +  json_data.column_last_name + '</a></td><td><a target="_blank" href="/edit.cfm?id=' + json_data.poc_id + '">' +  json_data.column_first_name + '</a></td><td>' +  json_data.column_title + '&nbsp;</td><td align="ceneter"><a href="/email.cfm?type=poc&id='+json_data.poc_id+'"  target="blank" ><img id="" border="0" align="center" title="Email '+ json_data.column_email +'" alt="Email '+ json_data.column_email +'" src="/images/mail-forward.png"/></a>&nbsp;</td><td>' +  json_data.column_phone + '&nbsp;</td></tr></tbody>';	
		});	
		
		results_text += '</td></tr>';
		results_text += '<tr><td class="s_nav" colspan="11" width="100%" style="border:none;" >'+
		'<table width="100%" class="search_nav"  style="border:none;"><tr><td align="left" width="40%"'+
		'  style="border:none;"><img src="/images/media-skip-backward.png" id="link_first" '+
		'align="absmiddle" alt="" class="img" alt="First" title="First" style="cursor:pointer;">&nbsp;<img src="/images/media-seek-backward.png" id="link_previous" align="absmiddle" class="img" alt="Previous" title="Previous" style="cursor:pointer;">&nbsp;&nbsp;&nbsp;<img src="/images/media-seek-forward.png" id="link_next" align="absmiddle" alt="" style="cursor:pointer;"class="img" alt="Next" title="Next">&nbsp;<img src="/images/media-skip-forward.png" id="link_last" align="absmiddle" alt="" class="img" alt="Last" title="Last" style="cursor:pointer;"> </td><td width="20%" align="center"  style="border:none;"><span class="" id="search_results_span">Results ' + json_search_data[0].lowest + '-' + json_search_data[0].highest + ' of ' + json_search_data[0].count + '</span></td><td align="right" width="40%"  style="border:none;"><div style="text-align:right;"><img src="/images/media-skip-backward.png" id="link_first2" align="absmiddle" class="img" alt="First" title="First" style="cursor:pointer;">&nbsp;<img src="/images/media-seek-backward.png" id="link_previous2" align="absmiddle" class="img" alt="Previous" title="Previous" style="cursor:pointer;">&nbsp;&nbsp;&nbsp;<img src="/images/media-seek-forward.png" id="link_next2" align="absmiddle"  class="img" alt="Next" title="Next" style="cursor:pointer;">&nbsp;<img src="/images/media-skip-forward.png" id="link_last2" align="absmiddle" class="img" alt="Last" title="Last" style="cursor:pointer;"></div></td></tr></table></td></tr>';
		
		results_text += '<tr><td class="s_nav" colspan="11" width="100%" style="border:none;" ><input type="button" value="Delete POC" name="delete_poc_00" id="delete_poc_00" class="button" ><span id="report_loading" class="loading"></span></td></tr></table>';
		$('#lowest_term').val(json_search_data[0].lowest);
		$('#highest_term').val(json_search_data[0].highest);
		$('#count').val(json_search_data[0].count) ;
		$("#"+results_div).html(results_text);
		$("#link_go").click(function() { searchPoc("get_first",sort_col,sort_dir);return false; });
		$("#link_first").click(function() { searchPoc("get_first",sort_col,sort_dir);return false; });
		$("#link_previous").click(function() { searchPoc("get_previous",sort_col,sort_dir);return false; });
		$("#link_next").click(function() { searchPoc("get_next",sort_col,sort_dir);return false; });
		$("#link_last").click(function() { searchPoc("get_last",sort_col,sort_dir);return false; });
		$("#link_first2").click(function() { searchPoc("get_first",sort_col,sort_dir);return false; });
		$("#link_previous2").click(function() { searchPoc("get_previous",sort_col,sort_dir);return false; });
		$("#link_next2").click(function() { searchPoc("get_next",sort_col,sort_dir);return false; });
		$("#link_last2").click(function() { searchPoc("get_last",sort_col,sort_dir);return false; });	
		
		$("#sort_state").click(function() { searchPoc("get_first","state",$('#sort_direction').val()) });
		$("#sort_city").click(function() { searchPoc("get_first","city",$('#sort_direction').val()) });
		$("#sort_gov_type").click(function() { searchPoc("get_first","gov_type",$('#sort_direction').val()) });
		$("#sort_gov_type_name").click(function() { searchPoc("get_first","company",$('#sort_direction').val())});
		$("#sort_address").click(function() { searchPoc("get_first","address",$('#sort_direction').val())});
		$("#sort_last_name").click(function() { searchPoc("get_first","last_name",$('#sort_direction').val())});
		$("#sort_first_name").click(function() { searchPoc("get_first","first_name",$('#sort_direction').val())});
		$("#sort_title").click(function() { searchPoc("get_first","title",$('#sort_direction').val())});
		$('#select_all').click(function() { 
			var checked_status = this.checked;
			$("input[id^=_filter_]").each(function()
			{
				this.checked = checked_status;
			});							
		});
		$("#sort_email").click(function() { searchPoc("get_first","email",$('#sort_direction').val())});
        binding();
		},
		complete: function(){ 
		
		},
        error: function(json){  
			$("#"+results_div).html('<table id="" border="1" cellspacing="0" cellpadding="0" class="datagrid"><thead><tr><th  width="2%"><input type="checkbox" id="select_all" name="select_all"  />'+
			'</th><th><div id="sort_state">Location</div></th><th ><div id="sort_city">Jurisdiction</div></th><th  >'+
			'<div id="sort_gov_type">Agency Type</div></th><th><div id="sort_gov_type_name">Agency Name</div></th><th >'+
			'<div id="sort_address">Agency Physical Address</div></th><th ><div id="sort_last_name">Contact Last Name</div>'+
			'</th><th ><div id="sort_first_name">Contact First Name</div></th><th ><div id="sort_title">Contact Title</div>'+
			'</th><th ><div id="sort_email">Contact Email</div></th><th ><div >Contact Phone </div></th></tr></thead><tbody>'+
			'<tr><td colspan="11" id="loading_area">No Search Results</td></tr></table>');
		}
    });
}

var loadPage = function() {		
	$('#'+results_div).html('<table id="" border="1" cellspacing="0" cellpadding="0" class="datagrid"><thead><tr><th  width="2%"><input type="checkbox" id="select_all" name="select_all"  /></th><th><div id="sort_state">Location</div></th><th ><div id="sort_city">Jurisdiction</div></th><th  ><div id="sort_gov_type">Agency Type</div></th><th><div id="sort_gov_type_name">Agency Name</div></th><th ><div id="sort_address">Agency Physical Address</div></th><th ><div id="sort_last_name">Contact Last Name</div></th><th ><div id="sort_first_name">Contact First Name</div></th><th ><div id="sort_title">Contact Title</div></th><th ><div id="sort_email">Contact Email</div></th><th ><div >Contact Phone </div></th></tr></thead><tbody><tr><td colspan="11" id="loading_area"><div align="center" class="Content_Margin">Loading Information<br />'+progress_bar(32,32,'','')+'</div></td></tr></table>');	

	$('#sort_').val( sort_col);
	$('#sort_direction').val(sort_dir);
	sort_col = $('#sort_').val( );
	sort_dir = $('#sort_direction').val();
	
    var tmp = $('#sort_direction').val();
		if (tmp == "desc")
		{
			$('#sort_direction').val("asc") ;
		}
		else 
		{
			$('#sort_direction').val("desc");
		}

		$.ajax({
		type: 'GET',
		url: "/api/ajax_admin_delete_poc.cfm?action=get_first"+
		"&search_term_2="+$('#name').val()+
		"&lowest_term="+$('#lowest_term').val()+
		"&highest_term="+$('#highest_term').val()+
		"&count="+$('#count').val()+
		"&sort_column="+$('#sort_').val()+
		"&sort_direction="+$('#sort_direction').val(),
     dataType: "json",
     success: function(json) {
		var json_data = [];
		var json_search_data = [];
		json_data = json.result;
		json_search_data = json.search_info;
		
		results_text = '';
		
		results_text += '<table id="" border="1" cellspacing="0" cellpadding="0" class="datagrid"><thead><tr><th  width="2%"><input type="checkbox" id="select_all" name="select_all" class="checkbox_results" /></th><th><div id="sort_state">Location</div></th><th ><div id="sort_city">Jurisdiction</div></th><th  ><div id="sort_gov_type">Agency Type</div></th><th><div id="sort_gov_type_name">Agency Name</div></th><th ><div id="sort_address">Agency Physical Address</div></th><th ><div id="sort_last_name">Contact Last Name</div></th><th ><div id="sort_first_name">Contact First Name</div></th><th ><div id="sort_title">Contact Title</div></th><th ><div id="sort_email">Contact Email</div></th><th ><div id="sort_phone">Contact Phone </div></th></tr></thead>';

        $.map(json_data,function(json_data){    
				results_text += '<tbody><tr><td align="center" valign="absmiddle"><input type="checkbox" class="checkbox_results" id="_filter_' +  json_data.poc_id + '" name="_filter_' +  json_data.poc_id + '" value="' +  json_data.poc_id + '"/></td><td>' +  json_data.column_state + '&nbsp;</td><td>' +  json_data.column_city + '&nbsp;</td><td>' +  json_data.column_gov_type + '&nbsp;</td><td>' +  json_data.column_company + '&nbsp;</td><td>' +  json_data.column_address + '&nbsp;</td><td><a target="_blank" href="/edit.cfm?id=' + json_data.poc_id + '" >' +  json_data.column_last_name + '</a></td><td><a target="_blank" href="/edit.cfm?id=' + json_data.poc_id + '">' +  json_data.column_first_name + '</a></td><td>' +  json_data.column_title + '&nbsp;</td><td align="ceneter"><a href="/email.cfm?type=poc&id='+json_data.poc_id+'"  target="blank" ><img id="" border="0" align="center" title="Email '+ json_data.column_email +'" alt="Email '+ json_data.column_email +'" src="/images/mail-forward.png"/></a>&nbsp;</td><td>' +  json_data.column_phone + '&nbsp;</td></tr></tbody>';	
		});	
		
		results_text += '</td></tr>';
		results_text += '<tr><td class="s_nav" colspan="11" width="100%" style="border:none;" >'+
		'<table width="100%" class="search_nav"  style="border:none;"><tr><td align="left" width="40%"'+
		'  style="border:none;"><img src="/images/media-skip-backward.png" id="link_first" '+
		'align="absmiddle" alt="" class="img" alt="First" title="First" style="cursor:pointer;">&nbsp;<img src="/images/media-seek-backward.png" id="link_previous" align="absmiddle" class="img" alt="Previous" title="Previous" style="cursor:pointer;">&nbsp;&nbsp;&nbsp;<img src="/images/media-seek-forward.png" id="link_next" align="absmiddle" alt="" style="cursor:pointer;"class="img" alt="Next" title="Next">&nbsp;<img src="/images/media-skip-forward.png" id="link_last" align="absmiddle" alt="" class="img" alt="Last" title="Last" style="cursor:pointer;"> </td><td width="20%" align="center"  style="border:none;"><span class="" id="search_results_span">Results ' + json_search_data[0].lowest + '-' + json_search_data[0].highest + ' of ' + json_search_data[0].count + '</span></td><td align="right" width="40%"  style="border:none;"><div style="text-align:right;"><img src="/images/media-skip-backward.png" id="link_first2" align="absmiddle" class="img" alt="First" title="First" style="cursor:pointer;">&nbsp;<img src="/images/media-seek-backward.png" id="link_previous2" align="absmiddle" class="img" alt="Previous" title="Previous" style="cursor:pointer;">&nbsp;&nbsp;&nbsp;<img src="/images/media-seek-forward.png" id="link_next2" align="absmiddle"  class="img" alt="Next" title="Next" style="cursor:pointer;">&nbsp;<img src="/images/media-skip-forward.png" id="link_last2" align="absmiddle" class="img" alt="Last" title="Last" style="cursor:pointer;"></div></td></tr></table></td></tr>';
		
		results_text += '<tr><td class="s_nav" colspan="11" width="100%" style="border:none;" ><input type="button" value="Delete POC" name="delete_poc_00" id="delete_poc_00" class="button" ><span id="report_loading" class="loading"></span></td></tr></table>';
		$('#lowest_term').val(json_search_data[0].lowest);
		$('#highest_term').val(json_search_data[0].highest);
		$('#count').val(json_search_data[0].count) ;
		$("#"+results_div).html(results_text);
		$("#link_go").click(function() { searchPoc("get_first",sort_col,sort_dir);return false; });
		$("#link_first").click(function() { searchPoc("get_first",sort_col,sort_dir);return false; });
		$("#link_previous").click(function() { searchPoc("get_previous",sort_col,sort_dir);return false; });
		$("#link_next").click(function() { searchPoc("get_next",sort_col,sort_dir);return false; });
		$("#link_last").click(function() { searchPoc("get_last",sort_col,sort_dir);return false; });
		$("#link_first2").click(function() { searchPoc("get_first",sort_col,sort_dir);return false; });
		$("#link_previous2").click(function() { searchPoc("get_previous",sort_col,sort_dir);return false; });
		$("#link_next2").click(function() { searchPoc("get_next",sort_col,sort_dir);return false; });
		$("#link_last2").click(function() { searchPoc("get_last",sort_col,sort_dir);return false; });	
		
		$("#sort_state").click(function() { searchPoc("get_first","state",$('#sort_direction').val()) });
		$("#sort_city").click(function() { searchPoc("get_first","city",$('#sort_direction').val()) });
		$("#sort_gov_type").click(function() { searchPoc("get_first","gov_type",$('#sort_direction').val()) });
		$("#sort_gov_type_name").click(function() { searchPoc("get_first","company",$('#sort_direction').val())});
		$("#sort_address").click(function() { searchPoc("get_first","address",$('#sort_direction').val())});
		$("#sort_last_name").click(function() { searchPoc("get_first","last_name",$('#sort_direction').val())});
		$("#sort_first_name").click(function() { searchPoc("get_first","first_name",$('#sort_direction').val())});
		$("#sort_title").click(function() { searchPoc("get_first","title",$('#sort_direction').val())});
		$('#select_all').click(function() { 
			var checked_status = this.checked;
			$("input[id^=_filter_]").each(function()
			{
				this.checked = checked_status;
			});							
		});
		
		$("#sort_email").click(function() { searchPoc("get_first","email",$('#sort_direction').val())});
		binding();
		},
		complete: function(){ 
		
		},
     error: function(json){  
			$("#"+results_div).html('<table id="" border="1" cellspacing="0" cellpadding="0" class="datagrid"><thead><tr><th  width="2%"><input type="checkbox" id="select_all" name="select_all"  />'+
			'</th><th><div id="sort_state">Location</div></th><th ><div id="sort_city">Jurisdiction</div></th><th  >'+
			'<div id="sort_gov_type">Agency Type</div></th><th><div id="sort_gov_type_name">Agency Name</div></th><th >'+
			'<div id="sort_address">Agency Physical Address</div></th><th ><div id="sort_last_name">Contact Last Name</div>'+
			'</th><th ><div id="sort_first_name">Contact First Name</div></th><th ><div id="sort_title">Contact Title</div>'+
			'</th><th ><div id="sort_email">Contact Email</div></th><th ><div >Contact Phone </div></th></tr></thead><tbody>'+
			'<tr><td colspan="11" id="loading_area">No Search Results</td></tr></table>');
		  }
    });
}

var binding = function() {
$("#delete_poc_00").bind("click",function() {  
			var poc_list = '';
			
			$("input[id^='_filter_']").each(function()
			{				
				(this.checked) ? poc_list += this.id.split('_filter_').join(",") : poc_list += '';
			});
      $.ajax({
          type: 'GET',
          url: "/api/ajax_admin_delete_poc.cfm?action=delete_poc"+
          "&id="+poc_list,
          dataType: "json",
          success: function(json) {
                      
          },
          complete: function() {
            loadPage();     
          },
          error: function(json) {                
          }                
       });
	
	});
}
$(function() {
	Functional.install();
  
    loadPage();
	$("#link_go").click(function() {searchPoc("get_first",sort_col,sort_dir)});
	
	
});